Plotly in R

NOWHERE

July 13, 2017

Introduction

Plotly is more than an r package and in fact, it is a general data visualization platform.

Today, let’s focus on its r lib– plotly.

To get our hands dirty, we need to download the package in r.

if(!require(plotly)) {
      install.packages("plotly")
      # or download from github
      # devtools::install_github("ropensci/plotly")
}
# load library
library(plotly)

Main Function

In plotly, the main function is plot_ly.

function (data = data.frame(), ..., type = NULL, color, colors = NULL,
    alpha = 1, symbol, symbols = NULL, size, sizes = c(10, 100),
    linetype, linetypes = NULL, split, frame, width = NULL, height = NULL,
    source = "A")
NULL

Main Function (cont’d)

Examples

I will demonstrate several examples here to show what plotly can do for us with r. You can also access this cheatsheet to plot with plotly quickly.

First, let’s generate some sample data.

set.seed(123)
x <- runif(n = 100, min = 1, max = 100)
y <- runif(n = 100, min = 1, max = 100)
y_new <- runif(n = 100, min = 1, max = 100)
library(dplyr)
plot_ly (x = x, y = x+4, type = 'scatter' ,mode = 'lines')
A
plot_ly (x = x, y = y,  type = 'scatter' ,mode = 'markers')
plot_ly (x = c(1, 2, 3), y = c(5, 6, 7),  type = 'bar')
plot_ly (x = x, y = y, type = 'scatter' ,mode = 'markers',
         size = runif(n = 100, min = 1, max = 10),
         marker = list(color = sample(c("red","green","orange","pink","blue"),
                                      size = 100,replace = TRUE)))
plot_ly (z = volcano, type="heatmap")
plot_ly (x =  x[order(x)] , y =(x[order(x)])^3, type = "scatter",
         mode = "lines", fill = "tozeroy")
plot_ly (x =  rnorm(n = 100000,mean = 0,sd = 1) , type="histogram")
plot_ly (y =  rnorm(n = 100,mean = 0,sd = 1) , type="box") %>% 
      add_trace(y=rnorm(n=100,mean = 0,sd = 2)) %>% 
      add_trace(y=rnorm(n=100,mean = 0,sd = 4)) %>% 
      add_trace(y=rnorm(n=100,mean = 0,sd = 3)) %>% 
      add_trace(y=rnorm(n=100,mean = 0,sd = 5))
plot_ly (x = rnorm(n = 100000,mean = 0,sd = 1), y = rnorm(n = 100000,mean = 0,sd = 1),
         type="histogram2d")
locs <- ggmap::geocode(location = c("Beijing, China", "London, England, United Kingdom",
                                    "New York, NY, United States"))
plot_ly (lon = locs$lon, lat = locs$lat, type = 'scattergeo', mode = "markers",
         marker = list(color = c("red", "salmon", "green"), size = c( 30, 40 ,50)))
plot_ly (type = 'choropleth',locations = c('AZ','PA','CA'), locationmode = 'USA-states',
         colorscale = 'Viridis', z = c(10, 20 ,30)) %>% layout(geo = list(scopre = 'usa'))
plot_ly (type = 'scattergeo', mode = "markers", lon = locs$lon, lat = locs$lat,
         text =c("Beijing, China", "London, England, United Kingdom",
                 "New York, NY, United States"),
         marker = list(color = c("red", "salmon", "green"), size = c( 30, 40 ,50)))
plot_ly (type = 'surface',z= ~volcano)
plot_ly (type = 'scatter3d', x = c(9, 8, 5, 1), y = c(1, 2, 4, 8),
         z = c(11, 8, 15, 3), mode = "lines")
plot_ly (type = 'scatter3d', x = x, y = y, z = runif(100, 0, 100),
         mode = 'markers', size = runif(n = 100, min = 1, max = 10),
         marker = list(color = sample(c("red","green","orange"),size = 100,replace = TRUE)))
axis_template <- list(showgrid = F, zeroline = F, nticks = 20,
                      showline = T, title = "AXIS", mirror = "all")
plot_ly (x =x, y = y, type = "scatter", mode = "markers") %>% 
      layout(xaxis = axis_template, yaxis = axis_template)
plot_ly (x =x, y = y, type = "scatter", mode = "markers", name = "1") %>% 
      add_trace(x = x, y = y_new, name = "2") %>%
      layout(legend = list( x = 1, y = 1, bgcolor = "white"))